package com.yjf.ljr.view;

import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.yjf.ljr.R;

public class DateDialog extends Dialog {

	protected Context mContext;
	protected DateDialogListener dateDialogListener;
	private String fromDate, toDate;
	private String defaultFormat = "yyyy-MM-dd";
	private ContentResolver resolver;

	public DateDialog(Context context, String fromDate, String toDate, DateDialogListener dateDialogListener, ContentResolver resolver) {
		super(context, R.style.dialog);
		this.mContext = context;
		this.dateDialogListener = dateDialogListener;
		this.fromDate = fromDate;
		this.toDate = toDate;
		this.resolver = resolver;
		if (resolver != null) {
			defaultFormat = Settings.System.getString(resolver, Settings.System.DATE_FORMAT);
			Settings.System.putString(resolver, Settings.System.DATE_FORMAT, "yyyy-MM-dd");
		}
	}

	public DateDialog(Context context, DateDialogListener dateDialogListener, ContentResolver resolver) {
		super(context, R.style.dialog);
		this.mContext = context;
		this.dateDialogListener = dateDialogListener;
		this.resolver = resolver;
		if (resolver != null) {
			defaultFormat = Settings.System.getString(resolver, Settings.System.DATE_FORMAT);
			Settings.System.putString(resolver, Settings.System.DATE_FORMAT, "yyyy-MM-dd");
		}
	}
	
	@Override
	public void dismiss() {
		if (resolver != null) {
			Settings.System.putString(resolver, Settings.System.DATE_FORMAT, defaultFormat);
		}
		super.dismiss();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.datedialog);
		((TextView) findViewById(R.id.tvTitle_titlelayout)).setText("就读时间");
		ImageButton btnLeft = (ImageButton)findViewById(R.id.btnLeft_titlelayout);
		btnLeft.setImageResource(R.drawable.back_selector);
		btnLeft.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		findViewById(R.id.btnRight_titlelayout).setVisibility(View.INVISIBLE);

		final DatePicker fromDatePicker = (DatePicker) findViewById(R.id.dpfrom_datedialog);
		final DatePicker toDatePicker = (DatePicker) findViewById(R.id.dpto_datedialog);
		// ((ViewGroup)(((ViewGroup)
		// fromDatePicker.getChildAt(0)).getChildAt(0))).getChildAt(1).setVisibility(View.GONE);
		// ((ViewGroup)(((ViewGroup)
		// fromDatePicker.getChildAt(0)).getChildAt(0))).getChildAt(2).setVisibility(View.GONE);
		// ((ViewGroup)(((ViewGroup)
		// toDatePicker.getChildAt(0)).getChildAt(0))).getChildAt(1).setVisibility(View.GONE);
		// ((ViewGroup)(((ViewGroup)
		// toDatePicker.getChildAt(0)).getChildAt(0))).getChildAt(2).setVisibility(View.GONE);

		hideMonth(fromDatePicker);
		hideMonth(toDatePicker);
		int sysVersion = Integer.parseInt(VERSION.SDK);
		
		if (fromDate != null && !"".equals(fromDate) && !"0".equals(fromDate)) {
//			if (sysVersion >= 15) {
//				fromDatePicker.updateDate(Integer.valueOf(fromDate) + 1, 1, 1);
//			} else {
				fromDatePicker.updateDate(Integer.valueOf(fromDate),1 , 1);
//			}
		} else {
			fromDatePicker.updateDate(fromDatePicker.getYear() - 3, 1, 1);
		}
		if (toDate != null && !"".equals(toDate) && !"0".equals(toDate)) {
//			if (sysVersion >= 15) {
//				toDatePicker.updateDate(Integer.valueOf(toDate) + 1, 1, 1);
//			} else {
				toDatePicker.updateDate(Integer.valueOf(toDate), 1, 1);
//			}
		}
		findViewById(R.id.btnconfirm_datedialog).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (fromDatePicker.getYear() > toDatePicker.getYear()) {
					Toast.makeText(mContext, "入学时间不能大于毕业时间", Toast.LENGTH_SHORT).show();
				} else {
					dateDialogListener.onConfirm(String.valueOf(fromDatePicker.getYear()), String.valueOf(toDatePicker.getYear()));
					dismiss();
				}
			}
		});
		findViewById(R.id.btncancel_datedialog).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
	}
	
	public void hideMonth(DatePicker mDatePicker) {
		if (mDatePicker != null) {
			int sysVersion = Integer.parseInt(VERSION.SDK);
			if (sysVersion >= 15) {
				((ViewGroup) (((ViewGroup) mDatePicker.getChildAt(0)).getChildAt(0))).getChildAt(1).setVisibility(View.GONE);
				((ViewGroup) (((ViewGroup) mDatePicker.getChildAt(0)).getChildAt(0))).getChildAt(2).setVisibility(View.GONE);
			} else {
				((ViewGroup) mDatePicker.getChildAt(0)).getChildAt(1).setVisibility(View.GONE);
				((ViewGroup) mDatePicker.getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
			}
		}
	}

	public interface DateDialogListener {
		public void onConfirm(String fromDate, String toDate);
	}

}
